package com.tourlaid.mamagerment.service.impl

import com.tourlaid.mamagerment.entity.StockIn
import com.tourlaid.mamagerment.enums.TldExceptionEnum
import com.tourlaid.mamagerment.form.StockInForm
import com.tourlaid.mamagerment.mapper.StockInMapper
import com.tourlaid.mamagerment.service.StockInService
import com.tourlaid.mamagerment.util.LoginUtil
import com.tourlaid.mamagerment.util.TldException
import org.springframework.beans.BeanUtils
import org.springframework.stereotype.Service
import java.util.*
import javax.annotation.Resource

/**
 * Created by xiaoyongge on 2017/7/23.
 */
@Service("stockInService")
class StockInServiceImpl : StockInService {

    @Resource
    var stockInMapper: StockInMapper? = null

    override fun queryForList(): List<StockIn>? {
        return stockInMapper!!.queryForList()
    }

    override fun insert(controlNo: String?, goodsId: Long?, supplierId: Long?, stock: Int?) {
        goodsId ?: throw TldException(TldExceptionEnum.GOODSID_NULL)
        supplierId ?: throw TldException(TldExceptionEnum.SUPPLIERID_NULL)
        controlNo ?: throw TldException(TldExceptionEnum.CONTROLNO_NULL)
        stock ?: throw TldException(TldExceptionEnum.STOCK_NULL)
        var stockIn = stockInMapper!!.getByControlNo(controlNo)
        if (stockIn != null) {
            throw TldException(TldExceptionEnum.CONTROLNO_EXIST)
        }
        var newStockIn = StockIn()
        newStockIn.controlNo = controlNo
        newStockIn.goodsId = goodsId
        newStockIn.supplierId = supplierId
        newStockIn.stock = stock
        newStockIn.creator = LoginUtil.userId
        newStockIn.editor = LoginUtil.userId
        stockInMapper!!.insert(newStockIn)
    }

    override fun getById(id: Long): StockIn? {
        return stockInMapper!!.getById(id)
    }

    override fun update(form: StockInForm) {
        form.id ?: throw TldException(TldExceptionEnum.ID_NULL)
        form.goodsId ?: throw TldException(TldExceptionEnum.GOODSID_NULL)
        form.supplierId ?: throw TldException(TldExceptionEnum.SUPPLIERID_NULL)
        form.controlNo ?: throw TldException(TldExceptionEnum.CONTROLNO_NULL)
        form.stock ?: throw TldException(TldExceptionEnum.STOCK_NULL)
        var oldStockIn = stockInMapper!!.getByControlNo(form.controlNo!!)
        if (oldStockIn != null && !Objects.equals(oldStockIn.id, form.id)) {
            throw TldException(TldExceptionEnum.CONTROLNO_EXIST)
        }
        var newStockIn = StockIn()
        BeanUtils.copyProperties(form, newStockIn)
        newStockIn.editor = LoginUtil.userId
        stockInMapper!!.update(newStockIn)
    }

    override fun delete(id: Long?) {
        stockInMapper!!.delete(id, LoginUtil.userId)
    }

    override fun queryByGoodsIds(goodsIdList: List<Long?>): List<StockIn> {
        return stockInMapper!!.queryByGoodsIds(goodsIdList)
    }

    override fun addStock(id: Long?, stock: Int?) {
        stockInMapper!!.addStock(id, stock)
    }

    override fun queryByGoodsId(goodsId: Long): List<StockIn> {
        return stockInMapper!!.queryByGoodsId(goodsId);
    }

    override fun minusStock(id: Long?, stock: Int?) {
        stockInMapper!!.minusStock(id, stock);
    }

}